package Week6;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;



//====================================================================================
//GUI class
//====================================================================================

//A over-simplified GUI for drawing.
public class MinimalDrawingGUI extends JPanel {

	private PointBuffer pb;
	private PolyLine pln;
	private Point p;
	
	
	public MinimalDrawingGUI() {
		//Feel free to ignore, this just calls the constructor of JPanel to enable  DoubleBuffering to avoid flickering.
		super(true); 
		
		//Set the (preferred) size of the panel
		setPreferredSize(new Dimension(500,500));
	}

	
	public Point getP() {
		return p;
	}

	public void setP(Point p) {
		this.p = p;
	}
	
	
	public PointBuffer getPb() {
		return pb;
	}

	public void setPb(PointBuffer pb) {
		this.pb = pb;
	}	
	

	public PolyLine getPln() {
		return pln;
	}

	public void setPln(PolyLine pln) {
		this.pln = pln;
	}

	
	

//====================================================================================
//PAINT
//====================================================================================

	/*
	 * Java takes care of calling this method. For this reason we had to define MinimalDrawingGUI 
	 * as a subclass of JPanel (line 14). You do not need to call the method by hand. If, however, you
	 * change data in your model, e.g., the PointBuffer, you will have to call repaint().
	 * 
	 */
	public void paintComponent(Graphics g) {
        super.paintComponent(g);    // Paints all other stuff, e..g., background. (Is this defined in JPanel?)
        
        // Cast to a Graphics2D object to support affine transformations
        Graphics2D g2d = (Graphics2D) g;
        //      g2d.shear(0.53 0.15);     
        //      g2d.rotate(0.25);
        //      g2d.scale(2.0, -1.5);

        
        // A simple line example; keep in mind we use display coordinates!
        g2d.drawLine(100, 100, 300, 400);

        
        //Draw PointBuffer (Note: doubles are cast as ints)
        g2d.setColor(Color.red);
        g2d.drawOval(
        		(int) pb.getPt().getX(),
        		(int) pb.getPt().getY(), 
        		(int) pb.getRad(), 
        		(int) pb.getRad());

        
        //Draw point with oval of radius 5
        //and an offset of 5/2 to center the oval
        //on the corresponding display coordinate.
        g2d.setColor(Color.blue);        
        g2d.drawOval((int) p.getX()-(5/2),(int) p.getY()-(5/2), (int) 5, (int) 5);
    	
		
		//Draw polyline
        g2d.setColor(Color.GREEN);
        for(int i=0; i < pln.polypoints.size()-1; i++){
			g2d.drawLine(
	        		(int) pln.polypoints.get(i).getX(),
	        		(int) pln.polypoints.get(i).getY(),
	        		(int) pln.polypoints.get(i+1).getX(),
	        		(int) pln.polypoints.get(i+1).getY()
	        		);
			}
        
        	
        
    

        
	}


//====================================================================================
//MAIN Program (embedded)
//====================================================================================

	public static void main(String[] args) throws InterruptedException {

		
		MinimalDrawingGUI gui = new MinimalDrawingGUI();

		
		//===========================
		//Default GUI settings (begin)
		//... Frame size is still problematic. What does it depend upon? Objects?

		//You always need a frame to place other components such as panels or buttons
		JFrame frame = new JFrame("A minimalistic GUI for drawing");

		//Add an instance of the MinimalDrawingGUI to the frame.
		frame.add(gui);
		
		//Set the size, arrange components, and display the frame.
		frame.pack();
		frame.setVisible(true);
		
		//Exit on close
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Default GUI settings (end)
		//==========================

		
		
		
		
		
		//Construct objects and print to console
		//Usually, you would do this somewhere else. (Where?)
		
		//Construct PointBuffer
		gui.setPb(new PointBuffer(new Point(100,100), 20));
		System.out.println("");
		System.out.println("PointBuffer:");
		System.out.println("Center: " + gui.pb.getPt());
		System.out.println("Radius: " + gui.pb.getRad());

		//Construct Point
		gui.setP(new Point(200,200));
		System.out.println("");
		System.out.println("Point:");		
		System.out.println(gui.p);	
		
		//Construct PolyLine
		gui.setPln(new PolyLine()) ;
		gui.pln.add(new Point(200,200));
		gui.pln.add(new Point(200,400));
		gui.pln.add(new Point(700,400));
		gui.pln.add(new Point(700,200));		
		System.out.println("");
		System.out.println("Polyline:");
		for (int i=0; i < gui.pln.polypoints.size(); i++){
			System.out.println(gui.pln.polypoints.get(i));
		}
		
		
		

		
	
		
		
		//Animation of PointBuffer
		//(Note: center.setX and center.setY
		// acts upon PointBuffer pb
		// through its initial definition
		// using gui.getPb().getCenter() )
		Point center = gui.getPb().getPt();
		for(int i=0;i<450;i++){
			center.setX(i);
			center.setY(i);

			
			
			
		//======================================	
		//Default GUI animation settings (begin)
			
		//Just to make sure you see a change we introduce 10ms breaks per cycle.
		Thread.sleep(20);
			
		//Remove the repaint line and see what happens.
		gui.repaint();
		
		//Default GUI animation settings (end)
		//====================================		
		
		
		
		}
	}
}
